package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.igas.bomp.facade.BompMchntInspectService;
import cc.rengu.igas.bomp.facade.bean.MchntSignImageInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntInspectInfoManagerRequest;
import cc.rengu.igas.bomp.facade.response.MchntInspectInfoQueryResponse;
import cc.rengu.redp.bizimpl.enums.BompTxnNumEnum;
import cc.rengu.redp.bizimpl.upmp.domain.InspectTaskDetail;
import cc.rengu.redp.bizimpl.upmp.service.IInspectTaskDetailService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntBaseInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 刘玉亮.LL
 */
@Slf4j
@Api(tags = "商户管理:商户巡检记录查询")
@RestController
@RequestMapping(value = "/admin/merParam/inspectTaskDetail")
public class InspectTaskDetailController extends BaseController {
    @Autowired
    private IInspectTaskDetailService inspectTaskDetailService;

    @DubboReference(version = "${dubbo.protocol.version}", check = false)
    private BompMchntInspectService mchntInspectService;

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Autowired
    private IUpmpBMchntBaseInfoService bMchntBaseInfoService;
    //@RequiresPermissions("商户管理:巡检任务明细管理")
    @GetMapping(value = "/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "inspectTaskNo", value = "巡检任务编号",required =true, dataType = "String")
    })
    public Map<String,Object>  findByPage(HttpServletRequest request, QueryRequest  queryRequest) throws RedpException {
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        return getDataTable(inspectTaskDetailService.findPageBompList(request,queryRequest, user,mchntInspectService, bMchntBaseInfoService.genMerOrder(user.getInstId())));
    }
    
    //@RequiresPermissions("商户管理:巡检任务明细管理")
    @GetMapping(value = "/edit")
    public Map editShow(HttpServletRequest request, Model model) throws RedpException {
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        String instId = request.getParameter("instId");
        String optFlag = request.getParameter("optFlag") == null ? "" : request.getParameter("optFlag");
        result.put("instId", instId);
        result.put("optFlag", optFlag);

        // 商户巡检信息详情查询
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        MchntInspectInfoQueryResponse resp = inspectTaskDetailService.mchntInspectInfoQuery(request, user, mchntInspectService, bMchntBaseInfoService.genMerOrder(user.getInstId()));

        List<MchntSignImageInfoBean> mchntInspectImageInfoList =  resp.getMchntInspectImageInfoList();
        if(null != mchntInspectImageInfoList && mchntInspectImageInfoList .size() > 0){
            for(MchntSignImageInfoBean mchntSignImageInfoBean : mchntInspectImageInfoList){
                String imageType = mchntSignImageInfoBean.getImageType();
                if(imageType.contains("-")){
                    imageType = imageType.split("-")[1];
                }
                mchntSignImageInfoBean.setImageType(imageType);
            }
        }

        result.put("mchntInspectInfo", resp.getMchntInspectInfo());
        result.put("mchntSignImageInfoList", resp.getMchntSignImageInfoList());
        result.put("mchntInspectImageInfoList", resp.getMchntInspectImageInfoList());

        return result;
    }


    //@RequiresPermissions("商户管理:巡检任务明细管理")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required =true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "操作标识",required =true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "opt", value = "操作标识", required =false, dataType = "String")
    })
    public OperationResult editSave(@ModelAttribute("inspectTaskDetail") InspectTaskDetail entity, HttpServletRequest request, Model model) {
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String instId = request.getParameter("instId");
        String optFlag = request.getParameter("optFlag") == null ? "" : request.getParameter("optFlag");
        String opt = request.getParameter("opt") == null ? "" : request.getParameter("opt");
        // 组装请求报文
        MchntInspectInfoManagerRequest req = new MchntInspectInfoManagerRequest();
        req.setUserId(user.getId());
        req.setInspectTaskNo(entity.getInspectTaskNo());
        req.setMchntNo(entity.getMchntNo());
        req.setInspectType(entity.getInspectType());
        req.setInspectInstId(entity.getInspectInstId());
        req.setManagerId(entity.getManagerId());
        req.setInspectTime("");
        req.setHeader(CommonUtils.packageBompHeader(bMchntBaseInfoService.genMerOrder(user.getInstId()), BompTxnNumEnum.mchntInspectInfoManager.getType(), instId,null));
        if (optFlag.equals("chk")) {
            if (opt.equals("pass")) {
                req.setOprType("03");// 03-商户巡检信息审核
                req.setCheckStatus("10"); // 10-已巡检
            } else if (opt.equals("back")) {
                req.setOprType("03");// 03-商户巡检信息审核
                req.setCheckStatus("03"); // 03-巡检拒绝
                req.setAuditRefuseReason(entity.getInspectRefuseReason());
            }
        } else if (optFlag.equals("upd")) {
            req.setOprType("02");// 02-商户巡检信息修改
            req.setCheckStatus(entity.getInspectStatus());
        }
        // 商户巡检信息管理
        return inspectTaskDetailService.mchntInspectInfoManager(req, mchntInspectService);
    }

    //@RequiresPermissions("商户管理:巡检任务明细管理")
    @DeleteMapping("/{ids}")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.inspectTaskDetailService.getBaseMapper().deleteBatchIds(Arrays.asList(ids.split(StringPool.COMMA)));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }
}
